<?php

namespace App\Repository;

use App\Entity\Users;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Users>
 *
 * @method Users|null find($id, $lockMode = null, $lockVersion = null)
 * @method Users|null findOneBy(array $criteria, array $orderBy = null)
 * @method Users[]    findAll()
 * @method Users[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class UsersRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Users::class);
    }

    public function add(Users $entity, bool $flush = false): void
    {
        $this->getEntityManager()->persist($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(Users $entity, bool $flush = false): void
    {
        $this->getEntityManager()->remove($entity);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    /**
     * find user info by conds
     * @param array $conds
     * @return array
     */
    public function findUsersQuery(array $conds): Query
    {
        return $this->createQueryBuilder('u')
            ->where('u.is_active = :is_active')
            ->andWhere('u.is_member = :is_member')
            ->andWhere('u.last_login_at Between :last_login_at_start AND :last_login_at_end')
            ->andWhere('u.user_type IN (:user_type)')
            ->setParameter('is_active', $conds['is_active'])
            ->setParameter('is_member', $conds['is_member'])
            ->setParameter('last_login_at_start', $conds['last_login_at_start'])
            ->setParameter('last_login_at_end', $conds['last_login_at_end'])
            ->setParameter('user_type', $conds['user_type'])
            ->getQuery();
    }

    /**
     * find user info by conds
     * @param array $conds
     * @return array
     */
    public function findUsers(array $conds): array
    {
        $page = $conds['page'] < 1 ? 1 : $conds['page'];
        $limit = $conds['limit'] < 1 ? 1 : $conds['limit'];
        $offset = ($page - 1) * $limit;

        return $this->createQueryBuilder('u')
            ->where('u.is_active = :is_active')
            ->andWhere('u.is_member = :is_member')
            ->andWhere('u.last_login_at Between :last_login_at_start AND :last_login_at_end')
            ->andWhere('u.user_type IN (:user_type)')
            ->setParameter('is_active', $conds['is_active'])
            ->setParameter('is_member', $conds['is_member'])
            ->setParameter('last_login_at_start', $conds['last_login_at_start'])
            ->setParameter('last_login_at_end', $conds['last_login_at_end'])
            ->setParameter('user_type', $conds['user_type'])
            ->getQuery()
            ->setFirstResult($offset)
            ->setMaxResults($limit)
            ->getResult();
    }

}
